package com.map.application.headquarter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.Toolkit;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import javax.swing.JLabel;
import javax.swing.JTextField;
import com.jgoodies.forms.factories.FormFactory;
import com.map.database.Database;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class EditMap extends JFrame {
	private JTextField mapNameTextField;
	private JTextField mapTypeTextField;
	private JTextField costPriceTextField;
	private JTextField sellingPriceTextField;
	private JTextField quantityTextField;

	public EditMap(final String lastMapName, final String lastMapType, final String lastCostPrice, final String lastSellingPrice, final String lastQuantity) {
		setIconImage(Toolkit.getDefaultToolkit().getImage(EditMap.class.getResource("/resources/earth_icon.png")));
		setTitle("Edit Map");
		setBounds(100, 100, 450, 350);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		JPanel panel = new JPanel();
		getContentPane().add(panel);
		panel.setLayout(new FormLayout(new ColumnSpec[] { FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode("max(21dlu;default)"), FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode("max(127dlu;default):grow"),
				FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode("max(0dlu;default):grow"), }, new RowSpec[] { FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, }));

		JLabel mapNameLabel = new JLabel("Map Name");
		panel.add(mapNameLabel, "6, 4");

		mapNameTextField = new JTextField();
		mapNameTextField.setText(lastMapName);

		panel.add(mapNameTextField, "12, 4, fill, default");
		mapNameTextField.setColumns(10);

		JLabel mapTypeLabel = new JLabel("Map Type");
		panel.add(mapTypeLabel, "6, 8");

		mapTypeTextField = new JTextField();
		mapTypeTextField.setColumns(10);
		mapTypeTextField.setText(lastMapType);

		panel.add(mapTypeTextField, "12, 8, fill, default");

		JLabel costPriceLabel = new JLabel("Cost Price");
		panel.add(costPriceLabel, "6, 12");

		costPriceTextField = new JTextField();
		costPriceTextField.setColumns(10);
		costPriceTextField.setText(lastCostPrice);
		panel.add(costPriceTextField, "12, 12, fill, default");

		JLabel sellingPriceLabel = new JLabel("Selling Price");
		panel.add(sellingPriceLabel, "6, 16");

		sellingPriceTextField = new JTextField();
		sellingPriceTextField.setColumns(10);
		sellingPriceTextField.setText(lastSellingPrice);
		panel.add(sellingPriceTextField, "12, 16, fill, default");

		JLabel quantityLabel = new JLabel("Quantity");
		panel.add(quantityLabel, "6, 20");

		quantityTextField = new JTextField();
		quantityTextField.setColumns(10);
		quantityTextField.setText(lastQuantity);
		panel.add(quantityTextField, "12, 20, fill, default");

		JButton btnNewButton = new JButton("Edit");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					Database db = new Database();
					db.ConnectApplicationDB();					
					String updateQuery = "update map set mapName = '"+mapNameTextField.getText()+"',mapType='"+mapTypeTextField.getText()+"',costPrice = "+costPriceTextField.getText()+",sellingPrice = "+sellingPriceTextField.getText()+",quantity = "+quantityTextField.getText()+" where mapName = '"+lastMapName+"' and mapType='"+lastMapType+"' and costPrice = "+lastCostPrice+" and sellingPrice = "+lastSellingPrice+" and quantity = "+lastQuantity;
					db.exceuteQueryStatement(updateQuery);
					db.closeDB();
					HeadQuarterMainWindow.updateMapTable();
					JOptionPane.showMessageDialog(null, "Update Successfull !!", "alert", JOptionPane.INFORMATION_MESSAGE);
					dispose();
					
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(null, "Update Failed !!", "alert", JOptionPane.ERROR_MESSAGE);
					e1.printStackTrace();
				}				
			}
		});
		panel.add(btnNewButton, "7, 24, 5, 1");

		JButton btnNewButton_1 = new JButton("Clear");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearTextBox();
			}
		});
		panel.add(btnNewButton_1, "12, 24, center, default");

	}

	public void clearTextBox() {
		mapNameTextField.setText("");
		mapTypeTextField.setText("");
		costPriceTextField.setText("");
		sellingPriceTextField.setText("");
		quantityTextField.setText("");
	}

}
